package logic

import (
	"context"
	"fmt"
	"hui-hui/models"

	"hui-hui/hui_hui_rpc/hui_hui_rpc"
	"hui-hui/hui_hui_rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserDataLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserDataLogic {
	return &UpdateUserDataLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateUserDataLogic) UpdateUserData(in *hui_hui_rpc.UpdateUserDataRequest) (*hui_hui_rpc.UpdateUserDataResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	if models.DB.Where("id = ?", in.Id).Limit(1).Find(&user).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	if user.ID == 0 {
		return nil, fmt.Errorf("用户令牌无效，请重新登录或联系管理员")
	}
	user.Image = in.Image
	user.Name = in.Name
	user.Des = in.Des
	if models.DB.Updates(&user).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	return &hui_hui_rpc.UpdateUserDataResponse{}, nil
}
